package com.t235.gc.goods.controller;

import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Goods;
import com.t235.gc.goods.service.GoodService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/good")
public class GoodController {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    @Autowired
    private GoodService goodService;

    @PostMapping("/cha")
    public Object select(Integer isStock,Integer isFault){
        System.out.println(isStock);
        System.out.println(isFault);
        return goodService.selectCha(isStock, isFault);
    }

    @PostMapping("isoff")
    @ApiOperation("商品上下架")
    public AjaxResult isoff(Goods goods){
        if(goods.getIsOff() == 1){
            goods.setIsOff(2);
        }else{
            goods.setIsOff(1);
        }
        int goods1 = goodService.update(goods);
        if(goods1 >= 1){
            return AjaxResult.success(goods1);
        }
        return AjaxResult.error("10001");
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("删除商品")
    public AjaxResult delet(@PathVariable Long id){
        Integer num = goodService.delete(id);
        if(num >= 1){
            return AjaxResult.success(num);
        }
        return AjaxResult.error("10001");
    }

    @PutMapping("/update")
    public AjaxResult update(Goods goods,MultipartFile uploadFile){
        String realPath = "";
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if(!folder.isDirectory()){
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            String filePath = uploadFile.getOriginalFilename();
            goods.setGoodsPic(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int goods1 = goodService.update(goods);
        if(goods1 >= 1){
            return AjaxResult.success(goods1);
        }
        return AjaxResult.error("10001");
    }

    @PostMapping("/update")
    public AjaxResult insert(Goods goods,MultipartFile uploadFile){
        String realPath = "";
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if(!folder.isDirectory()){
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            String filePath = uploadFile.getOriginalFilename();
            goods.setGoodsPic(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int goods1 = goodService.insert(goods);
        if(goods1 >= 1){
            return AjaxResult.success(goods1);
        }
        return AjaxResult.error("10001");
    }

}
